# coding=utf-8

import collections
#import random
import questions

# 真真 
# 2024-02-06
# 《四五快读》
# 《改变历史的战争》

ques = [
    ('morning', '早上', 0),
    ('afternoon', '下午', 0),
    ('evening', '傍晚', 0),
    ('night', '晚上', 0),
    ('Good evening', '傍晚好', 0),
    ('Good night', '晚上好', 0),
    ('How are you?', '你好吗？', 0),
    ("I'm fine. Thank you.", '我很好。谢谢你。', 0),
    ('Good bye, Alice.', '再见，艾丽丝。', 0),
    ('tape', '胶带', 0),
    ('cape', '披肩；斗篷', 0),
    ('cane', '藤条；拐杖', 0),
    ('kite', '风筝', 0),
    ('pine', '橡树', 0),
    ('ripe', '成熟的', 0),
    ('fine', '良好', 0),
    ('home', '家', 0),
    ('bone', '骨头', 0),
    ('cone', '圆锥体', 0),
    ('rope', '粗绳；绳索', 0),
    ('rain', '雨', 0),
    ('nail', '钉子', 0),
    ('tail', '尾巴', 0),
    ('wait', '等候', 0),
    ('bee', '蜜蜂', 0),
    ('feet', '脚', 0),
    ('seed', '种子', 0),
    ('jeep', '吉普车', 0),
    ('light', '光', 0),
    ('night', '晚上', 0),
    ('high', '高', 0),
    ('right', '右边', 0),
    ('boat', '小船', 0),
    ('coat', '外套', 0),
    ('soap', '香皂', 0),
    ('road', '路', 0),
    ('blue', '蓝色', 0),
    ('glue', '胶水', 0),
    ('clue', '线索', 0),
    ('Tuesday', '星期二', 0),


    # ('', '', 0),
    # ('他', 'tā', 0),
    # ('爱', 'ài', 0),
    # ('季', 'jì', 0),
    # ('都', 'dū', 0),
    # ('游', 'yóu', 0),
    # ('戏', 'xì', 0),
    # ('跑', 'pǎo', 0),
    # ('要', 'yào', 0),
    # ('狗', 'gǒu', 0),
    # ('猫', 'māo', 0),
    # ('落', 'là', 0),
    # ('孩', 'hái', 0),
    # ('姐', 'jiě', 0),
    # ('妹', 'mèi', 0),
    # ('弟', 'dì', 0),
    # ('会', 'huì', 0),
    # ('着', 'zhuó', 0),
    # ('跳', 'tiào', 0),
    # ('亮', 'liàng', 0),
    # ('《改变历史的战争》', '《 gǎi biàn lì shǐ de zhàn zhēng 》', 1),
    # ('历史', 'lì shǐ', 1),
    # ('冒险', 'mào xiǎn', 1),
    # ('改变', 'gǎi biàn', 1),
    # ('战争', 'zhàn zhēng', 1),
]


# 1. groups
# 2. select ques
# 3. build ques
# 4. return ques

groups = collections.defaultdict(list)

for idx, q in enumerate(ques):
    groups[q[2]].append(idx)

# print(groups)

#items = [x for x in range(len(ques))]
#print(items)

# select n from ques

#def random_items(lst, n = None):
#    if n is None or n > len(lst) or n < 0:
#        n = len(lst)
#    items = [x for x in range(len(lst))]
#    sel = []
#    for i in range(n):
#        idx = int(random.random() * (len(items) - i))
#        sel.append(items[idx])
#        items[idx] = items[len(items) - 1 - i]
#    return [lst[x] for x in sel]
#
#
#def random_item_idxs(lst, n = None):
#    if n is None or n > len(lst) or n < 0:
#        n = len(lst)
#    items = [x for x in range(len(lst))]
#    sel = []
#    for i in range(n):
#        idx = int(random.random() * (len(items) - i))
#        sel.append(items[idx])
#        items[idx] = items[len(items) - 1 - i]
#    return [[x, lst[x]] for x in sel]

#n = len(ques)
#sel = []
#for i in range(n):
#    idx = int(random.random() * (len(items) - i))
#    sel.append(items[idx])
#    items[idx] = items[len(items) - 1 - i]

def generate_questions(n = 10, swap = False, option_count = 4, questions = None, groups = None):

    assert questions is not None
    assert groups is not None
    


    sel = random_item_idxs(questions, n)

    #print(sel)

    result = []

    for item in sel:
        r = []

        ques_idx, ques_item = item
        
        title = ques_item[0]
        opt = ques_item[1]
        group = ques_item[2]

        group_items = groups[group] # 选中同组元素
        group_items.remove(ques_idx) # 删除正确答案

        if len(group_items) < 3: # 组内不够 3 个元素
            for g_idx, g in groups.items():
                if g_idx != group and len(g) > 3:
                    group_items += list(g)

        wrong_items = random_items(group_items, 3) # 选 3 个错误答案
        opt_items = random_items([ques_idx] + wrong_items)
        correct_idx = opt_items.index(ques_idx)

        r.append(title) # 添加 title
        for i in opt_items:
            r.append(questions[i][1]) # 添加选项
        r.append(correct_idx)

        assert len(r) == 6

        result.append(r)


        # item = ques[item_idx]
        # r.append(item[0]) # ques
        # group = item[2] # group

    return result


def gen_ques(n, swap = False, option_count = 4):
    return questions.generate_questions(n, swap, option_count, ques, groups)

